#include "mainwindow.h"
#include <QtGui>
#include <iostream>

#include <QHeaderView>
#include <QItemSelectionModel>
#include <QStandardItemModel>
#include <QTableView>
#include <QtXml/QDomDocument>
#include <QtXml/QDomElement>
#include <QtXml/QDomNode>

#include "delegate.h"

//constructor
MainWindow::MainWindow(){
    createActions();
    createMenus();
    createToolBar();
    //this->setWindowIcon(QIcon("/home/piotr/wolfenstein.png"));
    showMaximized();
    //setMinimumSize(700,500);
    setEnabledActions(true,false,false,false,false,false);
    setWindowTitle("hexAnEditor[*]");

}

MainWindow::~MainWindow(){}

//buttons for menubar
void MainWindow::createActions(){
    openAction = new QAction(tr("&Open"), this);
    openAction->setIcon(QIcon("fileopen-256.png"));
    openAction->setIconVisibleInMenu(true);
    connect(openAction, SIGNAL(triggered()), this, SLOT(open()));

    saveAction = new QAction(tr("&Save"), this);
    saveAction->setIcon(QIcon("Save-256.png"));
    saveAction->setIconVisibleInMenu(true);
    connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));

    saveAsAction = new QAction(tr("Save As"), this);
    saveAsAction->setIcon(QIcon("Save-as-256.png"));
    saveAsAction->setIconVisibleInMenu(true);
    connect(saveAsAction, SIGNAL(triggered()), this, SLOT(saveAs()));

    compareAction = new QAction(tr("Open file to compare with"), this);
    compareAction->setIcon(QIcon("Document-Compare-256.png"));
    compareAction->setIconVisibleInMenu(true);
    connect(compareAction, SIGNAL(triggered()), this, SLOT(openFileToCompare()));

    templateXMLAction = new QAction(tr("Use XML file as template"), this);
    templateXMLAction->setIcon(QIcon("Xml-256.png"));
    templateXMLAction->setIconVisibleInMenu(true);
    connect(templateXMLAction, SIGNAL(triggered()), this, SLOT(openXML()));

    startToCompare = new QAction(tr("Start to compare files"), this);
    startToCompare->setIcon(QIcon("Start-256.png"));
    startToCompare->setIconVisibleInMenu(true);
    connect(startToCompare, SIGNAL(triggered()), this, SLOT(compareFiles()));
}

//menubar
void MainWindow::createMenus()
{
    fileMenu = menuBar()->addMenu(tr("&File"));

    fileMenu->addAction(openAction);
    fileMenu->addAction(saveAction);
    fileMenu->addAction(saveAsAction);
    fileMenu->addSeparator();
    fileMenu->addAction(compareAction);
    fileMenu->addAction(templateXMLAction);
    fileMenu->addAction(startToCompare);
}

void MainWindow::createToolBar()
{
    toolBar = addToolBar(tr("&File"));
    toolBar->setToolButtonStyle(Qt::ToolButtonFollowStyle);
    toolBar->addAction(openAction);
    toolBar->addAction(saveAction);
    toolBar->addAction(saveAsAction);
    toolBar->addSeparator();
    toolBar->addAction(compareAction);
    toolBar->addAction(templateXMLAction);
    toolBar->addAction(startToCompare);
}

//invoke open function from fileWidget
void MainWindow::open(){
    if(okToContinue()){
        file = new fileWidget(this);
        std::cout<<"1"<<std::endl;
        setCentralWidget(file);
        std::cout<<"2"<<std::endl;
        if( file->open())
            setEnabledActions(true,true,true,true,true,false);
    }
}

void MainWindow::openFileToCompare(){
    if( file->openFileToCompare())
        setEnabledActions(true, true, true, true, true, true);
}

void MainWindow::compareFiles(){
    file->compare();
    setWindowModified(false);
}

bool MainWindow::save(){
    return file->save();
}

bool MainWindow::saveAs(){
    return file->saveAs();
}

void MainWindow::openXML()
{
    file->openXML();
}

void MainWindow::setEnabledActions(bool openAction,
                       bool saveAction,
                       bool saveAsAction,
                       bool compareAction,
                       bool templateXMLAction,
                       bool startToCompare){
    this->openAction->setEnabled(openAction);
    this->saveAction->setEnabled(saveAction);
    this->saveAsAction->setEnabled(saveAsAction);
    this->compareAction->setEnabled(compareAction);
    this->templateXMLAction->setEnabled(templateXMLAction);
    this->startToCompare->setEnabled(startToCompare);
}

bool MainWindow::okToContinue(){
    if (isWindowModified()) {
        int r = QMessageBox::warning(this, tr("hexAnEditor"),
                                    tr("The document has been modified.\n"
                                    "Do you want to save your changes?"),
                                    QMessageBox::Yes | QMessageBox::Default,
                                    QMessageBox::No,
                                    QMessageBox::Cancel | QMessageBox::Escape);
        if (r == QMessageBox::Yes) {
            return save();
        } else if (r == QMessageBox::Cancel) {
            return false;
        }
    }
    return true;
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    if(okToContinue()){
        event->accept();
    }else
        event->ignore();
}
